package cn.itsource.basic.controller;

import cn.itsource.basic.util.AxajResult;
import cn.itsource.basic.util.FastDfsApiOpr;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {
    @PostMapping("/upload")
    public AxajResult upLoad(MultipartFile file) throws IOException {
        try {
            //获取文件名
            String filename = file.getOriginalFilename();
            //获取文件后缀名
            String extension = FilenameUtils.getExtension(filename);
            //把文件转化为byte数组
            byte[] bytes = file.getBytes();
            //上传完成后返回的url地址
            String url = FastDfsApiOpr.upload(bytes, extension);
            System.out.println(url);
            return new AxajResult().setObjResult(url);
        } catch (IOException e) {
            e.printStackTrace();
            return new AxajResult(false,e.getMessage());
        }
    }
}
